package com.wisdom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.wisdom.adapter.MyAdapter.ViewHolder;
import com.wisdom.http.HttpRequestUrl;
import com.wisdom.utils.MoveBg;


import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MenuFragment extends Fragment
{
	
    private List<Map<String, Object>> mData;

    


    private Activity activity;
    
    LocalActivityManager manager = null;
    
    RelativeLayout layout_news_main;
    
    View page;
    
    RelativeLayout layout_title_bar;
    TextView mainTitleTV;    
    
    ListView listView;
    MyAdapter adapter;
    
    
    @Override
    public void onAttach(Activity activity) {
            this.activity = activity;
            super.onAttach(activity);
    }
    
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //TextView textView = new TextView(getActivity());
        //textView.setBackgroundColor(0x4400ff00);
    	layout_news_main = (RelativeLayout) activity.findViewById(R.id.layout_news_main);
    	layout_title_bar = (RelativeLayout) activity.findViewById(R.id.layout_title_bar);
    	mainTitleTV = (TextView) activity.findViewById(R.id.mainTitleTV);
    	
    	
		manager = new LocalActivityManager(activity, true);
        manager.dispatchCreate(savedInstanceState);
    	
    	View view = inflater.inflate(R.layout.menu_frame, container, false);
    	
    	mData = getData();
    	
    	
        
        listView = (ListView) view.findViewById(R.id.MenuListView);
        //listView.setSelector(R.drawable.menu_sharp);  //取消list点击时一闪(闪一下)的问题

        adapter = new MyAdapter(activity);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(OnListViewItemClickListener);
        
        adapter.setSelectItem(0);
        
        //listView.setOnItemClickListener(this);

        
        return view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        
        
        Thread menuThread = new Thread(new MenuHandler());
		menuThread.start();					
        
        
        //mData = getData();
        //ImageView followmenu = (ImageView)  getActivity().findViewById(R.id.followmenu);	
        //followmenu.setOnClickListener(onFollowClickListener);
        /*
        ListView listView = (ListView) getActivity().findViewById(R.id.MenuListView);
        
        
        
        //生成动态数组，并且转载数据
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<30;i++)
        {
        	HashMap<String, String> map = new HashMap<String, String>();
        	map.put("ItemTitle", "This is Title.....");
        	map.put("ItemText", "This is text.....");
        	mylist.add(map);
        }
        //生成适配器，数组===》ListItem
        SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), //没什么解释
        		                                    mylist,//数据来源 
        		                                    R.layout.vlist,//ListItem的XML实现
        		                                    
        		                                    //动态数组与ListItem对应的子项        
        		                                    new String[] {"ItemTitle", "ItemText"}, 
        		                                    
        		                                    //ListItem的XML文件里面的两个TextView ID
        		                                    new int[] {R.id.ItemTitle,R.id.ItemText});
        //添加并且显示
        listView.setAdapter(mSchedule);
        
        
        
        MyAdapter adapter = new MyAdapter(getActivity());
        listView.setAdapter(adapter);
        */
        
        
        
    }
    
    /*
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
        System.out.println("MenuFragment-->onCreate");
        
        //setContentView(R.layout.layout_news);
        
    }
    */
    
    
    

/*
    @Override        
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {                 
		if (((ListView)parent).getTag() != null){
			((View)((ListView)parent).getTag()).setBackgroundDrawable(null);
		}

		((ListView)parent).setTag(view);
		view.setBackgroundResource(R.drawable.biz_pics_set_item_icon);               
    }
*/

    private OnItemClickListener OnListViewItemClickListener = new OnItemClickListener() 
    {

    	@Override
    	public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
    	{

    		
			Message message = new Message();
			Bundle bundle = new Bundle();
			bundle.putInt("position", position);
			message.setData(bundle);
			menuHandler.sendMessage(message);
    		
    		
			//listView.setSelector(R.drawable.follow_menu_selector);
			
    		
			adapter.setSelectItem(position);
    		adapter.notifyDataSetInvalidated();
    		
    		
    		
    		
    		/*
    		if (((ListView)parent).getTag() != null)
    		{
    			((View)((ListView)parent).getTag()).setBackgroundDrawable(null);
    		}

    		((ListView)parent).setTag(view);
    		view.setBackgroundResource(R.drawable.biz_pics_set_item_icon);
    		*/
    	}

    };   

    

	
	
    private List<Map<String, Object>> getData() 
    {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("title", "主页");
        map.put("info", "Index");
        map.put("img", R.drawable.index_left);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "跟帖");
        map.put("info", "Follow");
        map.put("img", R.drawable.follow_menu);
        list.add(map);

 

        map = new HashMap<String, Object>();
        map.put("title", "热图");
        map.put("info", "Picture");
        map.put("img", R.drawable.pic_left);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("title", "投票");
        map.put("info", "Vote");
        map.put("img", R.drawable.vote_left);
        list.add(map);
         
        return list;
    }	
	
    public final class ViewHolder
    {
        public ImageView img;
        public TextView title;
        public TextView info;
        //public Button viewBtn;
        
        
    } 



	public class MyAdapter extends BaseAdapter
	{
	
		private int itemID;
		private LayoutInflater mInflater;
		private Context context;
	
	
		public MyAdapter(Context context)
		{
			this.context = context;
			this.mInflater = LayoutInflater.from(context);
		}
	
		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return mData.size();
		}
	
	
	
		@Override
		public Object getItem(int arg0)
		{
			// TODO Auto-generated method stub
			return null;
		}
	
	
	
		@Override
		public long getItemId(int arg0)
		{
			// TODO Auto-generated method stub
			
			
			return 0;
		}
	
	
	
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) 
		{
			
			itemID = position;
			ViewHolder holder = null;
			if (convertView == null) 
			{
				holder = new ViewHolder();  
				convertView = mInflater.inflate(R.layout.menulist, null);
				holder.img = (ImageView)convertView.findViewById(R.id.img);
				holder.title = (TextView)convertView.findViewById(R.id.title);
				holder.info = (TextView)convertView.findViewById(R.id.info);
				//holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
				convertView.setTag(holder);
		
			}
			else 
			{
				holder = (ViewHolder)convertView.getTag();
	
			}
	
	
	
	
	
			holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
			holder.title.setText((String)mData.get(position).get("title"));
			holder.info.setText((String)mData.get(position).get("info"));
			/*
			holder.viewBtn.setOnClickListener(new View.OnClickListener() {
	
				@Override
				public void onClick(View v) {
					//showInfo();
					
					Log.e("onItemClick :", "onClick " + position);
				}
	
			});
	*/
			//convertView.setBackgroundColor(0x336699);
			/*
			if (position == 0)
			{
				convertView.setBackgroundResource((Integer)mData.get(position).get("img"));  
			}
			else if (position == 1)
			{
				convertView.setBackgroundResource(R.drawable.slidebar);  
			}
*/
			
			
			
			
			if (position == selectItem ) 
			{
				
				if (position == 0)
				{
					holder.title.setTextColor(0xffe61a1a);
					holder.info.setTextColor(0xffe61a1a);
					
					convertView.setBackgroundResource(R.drawable.menu_sharp_clicked_1);
				}
				else if (position == 1)
				{
					holder.title.setTextColor(0xfff66a0b);
					holder.info.setTextColor(0xfff66a0b);
					
					convertView.setBackgroundResource(R.drawable.menu_sharp_clicked_2);
				}
				else if (position == 2)
				{
					holder.title.setTextColor(0xff27a651);
					holder.info.setTextColor(0xff27a651);
					
					convertView.setBackgroundResource(R.drawable.menu_sharp_clicked_3);
				}			
				else if (position == 3)
				{
					holder.title.setTextColor(0xff00cff7);
					holder.info.setTextColor(0xff00cff7);
					
					convertView.setBackgroundResource(R.drawable.menu_sharp_clicked_4);
				}
				
				
				
				
				
				
				

				convertView.getBackground().setAlpha(80);
			} 
			else 
			{
				convertView.setBackgroundResource(R.drawable.menu_sharp);
				
				holder.title.setTextColor(0xffffffff);
				holder.info.setTextColor(0xffffffff);
				//convertView.setBackgroundColor(Color.TRANSPARENT);
			} 
			
			

			

			/*
			convertView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.e("onItemClick_____ :", "onClick " + position);
					
					
					Message message = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("position", position);
					message.setData(bundle);
					menuHandler.sendMessage(message);

				}
			});
	*/
	
	
			return convertView;
	
		}
		

		
		public void setSelectItem(int selectItem) 
		{
			this.selectItem = selectItem;
		}
		
		private int selectItem = -1;

	
	
	
	}
	
	
	
	class MenuHandler implements Runnable 
	{
		@Override
		public void run()
		{
						
				
	        /*
			// 通过调用handler来通知UI主线程更新UI,
			
				Message message = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("id", position);
				message.setData(bundle);
				menuHandler.sendMessage(message);
			*/
		
		}
	}

	public Handler menuHandler = new Handler() 
	{
		public void handleMessage(Message msg) 
		{
			//boolean isAuth = msg.getData().getBoolean("isAuth");
			
			
			int position = msg.getData().getInt("position");	
			
			

			
			layout_news_main.removeAllViewsInLayout();
			layout_news_main.removeAllViews();
			
			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			
			if (position == 0)
			{
				layout_title_bar.setVisibility(View.VISIBLE);
				mainTitleTV.setText("智慧碎片");
				
				
				//默认显示“头条”
				Intent intent = new Intent(getActivity(), TabNewsTopActivity.class);
				intent.putExtra("title", HttpRequestUrl.URL_NEWS_TOP);
				

				page = manager.startActivity("activity1", intent).getDecorView();
				//LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				

				//layout_news_main.removeAllViewsInLayout();
				//layout_news_main.removeAllViews();

				layout_news_main.addView(page, params);			

			}
			else if (position == 1)
			{
				layout_title_bar.setVisibility(View.GONE);
				mainTitleTV.setText("跟帖");
				
				
				//默认显示“头条”
				Intent intent = new Intent(getActivity(), TabFollowActivity.class);
				intent.putExtra("title", HttpRequestUrl.URL_NEWS_TOP);

				
				page = manager.startActivity("follow", intent).getDecorView();
				//page = getLocalActivityManager().startActivity("activity1", intent).getDecorView();
				//LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				



				layout_news_main.addView(page, params);						
				
			}
			else if (position == 2)
			{
				layout_title_bar.setVisibility(View.GONE);
				mainTitleTV.setText("热图");

				
				Intent intent = new Intent(getActivity(), TabPicActivity.class);

				page = manager.startActivity("picture", intent).getDecorView();

				layout_news_main.addView(page, params);						
				
			}
			else if (position == 3)
			{
				layout_title_bar.setVisibility(View.GONE);
				mainTitleTV.setText("投票");
	
				Intent intent = new Intent(getActivity(), TabVoteActivity.class);

				page = manager.startActivity("vote", intent).getDecorView();

				layout_news_main.addView(page, params);					
				
			}
		}
	};	
}
